#from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf
from tensorflow import keras
#from tensorflow import keras
#from tensorflow.keras import layers
import numpy as np
import sys
sys.path.append('../')
from utility.hdf5 import HDF5
from utility.keras_modal import DenseModel


if __name__ == "__main__":
    filePath="./game_recorders/game_recorders.h5"
    games=HDF5(filePath,mode='r')
    #type='cnn'
    type='pd_dense'
    model=DenseModel(dataGenerator=games.yeilds_data,boardSize=9,dataSize=1024*100,model=type)
    #'''
    #model.model_load('lj.h5')
    #model.model_eval()
    #weights = model.model.get_weights()
    #print(weights)
    '''
    npdata1=np.zeros((1,362)).reshape(1,362)
    pred1=model.model_predict(npdata1)
    npdata2=np.random.randint(-1,high=2,size=362).reshape(1,362)
    pred2=model.model_predict(npdata2)
    npdata3=np.random.randint(-1,high=2,size=362).reshape(1,362)
    pred3=model.model_predict(npdata3)
    npdata4=np.random.randint(-1,high=2,size=362).reshape(1,362)
    pred4=model.model_predict(npdata4)
    print(npdata1.flatten()[:-1].reshape(19,19))
    a=np.zeros((361,))
    a[pred1.argmax()]=1
    print(a.reshape(19,19))
    print(npdata2.flatten()[:-1].reshape(19,19))
    a=np.zeros((361,))
    a[pred2.argmax()]=1
    print(a.reshape(19,19))
    print(npdata3.flatten()[:-1].reshape(19,19))
    a=np.zeros((361,))
    a[pred3.argmax()]=1
    print(a.reshape(19,19))
    print(npdata4.flatten()[:-1].reshape(19,19))
    a=np.zeros((361,))
    a[pred4.argmax()]=1
    print(a.reshape(19,19))
    '''
    #'''
    #model.show_summary()
    model.model_compile()
    model.model_fit(batch_size=16*2,epochs=10000,earlystop=10,checkpoint=True)
    model.model_save(type+'.h5')
    #'''

    '''
    dset = dataset.batch(1).take(1)  #加take限制了只拿一次，batch是一次取多少数据出来
    for x,y,z  in dset:
        print(x,y,z)

    '''
    '''
    i=0
    for data in games.yeilds_data():
        print(i, ":", data[0][0].shape,data[0][1].shape)
        i+=1
        if i > 1:
            break
    '''
    '''
    model = tf.keras.Sequential()
    # Adds a densely-connected layer with 64 units to the model:
    model.add(layers.Dense(361, activation='relu'))
    # Add another:
    model.add(layers.Dense(1024, activation='relu'))
    model.add(layers.Dense(1024*3, activation='relu'))
    model.add(layers.Dense(1024*3, activation='relu'))
    model.add(layers.Dense(1024, activation='relu'))
    model.add(layers.Dense(512, activation='relu'))
    # Add an output layer with 10 output units:
    model.add(layers.Dense(361))
    model.compile(optimizer=tf.keras.optimizers.Adam(0.1),
              loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
    dataset = dataset.batch(8)
    model.fit(dataset, epochs=100)
    '''
    '''
    input1 = keras.layers.Input(shape=(362,))
    x1 = keras.layers.Dense(1024, activation='relu')(input1[:,:-1])
    #print(input1.shape,input1[:,-1:0].shape)
    #t=keras.layers.Reshape((1,))(input1[:,-1])
    #print(t.shape)
    added = keras.layers.concatenate([x1, input1[:,361:362]], axis=-1)
    x2 = keras.layers.Dense(512, activation='relu')(added)
    out = keras.layers.Dense(361)(x2)
    model = keras.models.Model(inputs=input1, outputs=out)
    keras.utils.plot_model(model, 'my_first_model.png')
    model.compile(optimizer=tf.keras.optimizers.Adam(0.1),
              loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
    dataset = dataset.batch(128,drop_remainder=True).prefetch(tf.data.experimental.AUTOTUNE)
    model.fit(dataset, epochs=1)
    '''
